home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18393 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.0 KB  |  213 lines

  1. Path: ix.netcom.com!netnews
  2. From: lewkbj@ix.netcom.com(leonel wizel )
  3. Newsgroups: comp.lang.c++
  4. Subject: help with homework
  5. Date: 20 Apr 1996 03:30:46 GMT
  6. Organization: Netcom
  7. Message-ID: <4l9lp6$cr1@reader2.ix.netcom.com>
  8. NNTP-Posting-Host: ix-nyc17-20.ix.netcom.com
  9. X-NETCOM-Date: Fri Apr 19  8:30:46 PM PDT 1996
  10.  
  11. To all of you experienced programmers.  I have some problems
  12. understanding the object concept and that is the reason that I need
  13. some help from you all experienced programmers.
  14.  
  15.  
  16. the problem that I have no I have to improve a class called Time
  17.  
  18.  
  19. I have to write a driver program that tests the tick member function in
  20. a loop that prints the time in standard format during each iteration of
  21. the loop to illustrate that the tick member function works correctly. 
  22. I have
  23.  
  24.  to be sure to test the following cases:
  25.  
  26.     incrememting into the next minute
  27.     incrementing into the next hour.
  28.     incrementing into the next day.(i.e., 11:59:59 PM to 12:00:00 AM).
  29.  
  30. then I have to modify and add a different class or functions to perfor
  31. error checking on the initializer values for data members month, day,
  32. and year. and provide a member function nextDay to increment day by
  33. one.  The Da
  34.  
  35. te object should always remain ina consisten state.   and write a
  36. driver program that tests the nextday function in a loop that prints
  37. the date during each iteration of the loop to illustrate that the next
  38. day function wo
  39.  
  40. rks correctly.  
  41.  
  42.     incrementing into the next month.
  43.     incrementing into the next year.
  44.  
  45. then modified Time and modified Date into one class called DateAndTime.
  46. modified the tick functio to call the next day function if the time is
  47. incremented into the next day.  modified function printStandard and
  48. PrintMilit
  49.  
  50. ary to output the date in addition to the time, and write a driver to
  51. test the new class DateAndTime; to test incrementing the time into the
  52. next day.
  53.  
  54.     then modify the set function in the program to return appropiate
  55. error values in an attempt is made to set a data member of an object of
  56. class Time to an invalid value.
  57.  
  58.  
  59. Thank you very much for your cooperation.
  60.  
  61.     This is that I have written so far.
  62.  
  63.     File no 1.
  64.  
  65.  
  66. //times3.h
  67.  
  68. #ifndef TIME3_H
  69. #define TIME3_H
  70.  
  71. class Time {
  72.     public:
  73.         Time(int = 0, int = 0, int = 0);  // constructor
  74.  
  75. //set functions
  76.  
  77.     void setTime(int, int, int);  //set hour, minute, second
  78.     void setHour(int);
  79.     void setMinute(int);
  80.     void setSecond(int);
  81.  
  82.    //get functions
  83.  
  84. int getHour();  //return hour
  85. int getMinute();  //return minute
  86. int getSecond();  //return second
  87.  
  88. void tick_increment();   // increments time
  89. void printMilitary();  //output military time
  90. void printStandar();  //output standard time
  91.  
  92. private:
  93.     int hour;
  94.     int minute;
  95.     int second;
  96. };
  97.  
  98. #endif
  99.  
  100.  
  101.             File No 2.
  102.  
  103. //this is file time3.cpp
  104. #include "time3.h"
  105. #include <iostream.h>
  106.  
  107.  
  108. //constructor function.
  109.  
  110. Time::Time(int hr, int min, int sec)  {setTime (hr, min, sec); }
  111.  
  112. void Time::setTime(int h, int m, int s)
  113. {
  114.     hour = (h >= 0 && h < 24) ? h : 0;   //check for negative values
  115.     minute = (m >= 0 && m < 60) ? m : 0;
  116.     second = (s >= 0 && s < 60) ? s : 0;
  117. }
  118. void Time::setMinute(int m)
  119.    
  120.  {minute = (m >= 60) ? m : 0;}
  121.  
  122. void Time::setSecond(int s)
  123.  
  124. {second = (s >= 60) ? s : 0;}
  125.  
  126. int Time::getHour()  {return hour; }
  127.  
  128. int Time::getMinute() { return minute;}
  129.  
  130. int Time::getSecond() { return second; }
  131.  
  132. void Time::tick_increment()
  133. {
  134.     if(second == 60)
  135.     {
  136.         second = 0;
  137.         minute++;
  138.     }
  139.         second++;
  140.     if (second == 60)
  141.        {
  142.         second = 0;
  143.         minute++;
  144.         }
  145.      if(minute == 60)
  146.      {
  147.         minute = 0;
  148.         hour++;
  149.       }
  150. }
  151.  
  152. void Time::printMilitary()
  153. {
  154.     cout << (hour < 10 ? "0" : "") << hour << ":"
  155.          << (minute < 10 ? "0" : "") << minute <<":"
  156.          << (second < 10 ? "0" : "") << second;
  157. }
  158. void Time::printStandard()
  159. {
  160.     cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":"
  161.          << (minute < 10 ? "0" : "") << minute << ":"
  162.          << (second < 10 ? "0" : "")  << second
  163.          << (hour < 12 ? " AM" : " PM");
  164. }
  165.  
  166.  
  167.         file No 3
  168.  
  169. #include <iostream.h>
  170. #include "time3.h"
  171. #include "time3.cpp"
  172.  
  173. main()
  174. {
  175.     Time t;
  176.     
  177.     t.setTime(4, 00, 31);
  178.         
  179.     for(long int i = 1; i <= 2; i++)
  180.     {
  181.         cout << "Hour " << t.getHour() << endl << endl;
  182.         cout << "Minute " <<t.getMinute() << endl;
  183.         cout << "Second" << t.getSecond() << endl << endl;
  184.         cout << "the initial time before incrementing is: " << endl;
  185.  
  186.     t.printStandard();
  187.     t.printMilitary();
  188.  
  189.     t.tick_increment();
  190.     
  191.     cout << " Hour " << t.getHour() << endl << endl;
  192.     cout << " Minute " << t.getMinute() << endl;
  193.     cout << " Second " << t.getSecond() << endl << endl;
  194.  
  195.     cout << "The time after incrementing is: " << endl;
  196.  
  197.     t.printStandard();
  198.     t.PrintMilitary();
  199.     }
  200. return 0;
  201. }
  202.  
  203.  
  204.  
  205. This are the files that I am working on it; but I encounter some
  206. difficulties improving all the classes to work respectively with the
  207. specifications all this code is preliminary.
  208.  
  209.  
  210.  
  211.     thank you very much to all of you for your suggestions.
  212.   
  213.